package com.zhaoxi.project.cms.blog.service.impl;

import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.project.cms.blog.domain.CmsBlog;
import com.zhaoxi.project.cms.blog.domain.CmsBlogTag;
import com.zhaoxi.project.cms.blog.domain.CmsBlogType;
import com.zhaoxi.project.cms.blog.mapper.CmsBlogMapper;
import com.zhaoxi.project.cms.blog.mapper.CmsBlogTagMapper;
import com.zhaoxi.project.cms.blog.mapper.CmsBlogTypeMapper;
import com.zhaoxi.project.cms.tag.domain.CmsTag;
import com.zhaoxi.project.cms.tag.mapper.CmsTagMapper;
import com.zhaoxi.project.cms.type.domain.CmsType;
import com.zhaoxi.project.cms.type.mapper.CmsTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 博客信息Service业务层处理
 *
 * @author zhaoxi
 * @date 2023-07-25
 */
@Service
public class CmsBlogServiceImpl {
    @Autowired
    private CmsBlogMapper cmsBlogMapper;
    @Autowired
    private CmsBlogTagMapper cmsBlogTagMapper;
    @Autowired
    private CmsBlogTypeMapper cmsBlogTypeMapper;
    @Autowired
    private CmsTagMapper cmsTagMapper;
    @Autowired
    private CmsTypeMapper cmsTypeMapper;
    /**
     * 查询博客信息
     *
     * @param id 博客信息主键
     * @return 博客信息
     */

    public CmsBlog selectCmsBlogById(Long id)
    {
        CmsBlog blog = cmsBlogMapper.selectCmsBlogById(id);
        //查询标签列表
        List<CmsBlogTag> blogTagList = cmsBlogTagMapper.selectBlogTagList(id);
        Long[] tagIds = new Long[blogTagList.size()];
        List<CmsTag> tags = new ArrayList<>();
        List<CmsType> types = new ArrayList<>();
        for (int i = 0; i<blogTagList.size();i++){
            CmsBlogTag cmsBlogTag = blogTagList.get(i);
            Long tagId = cmsBlogTag.getTagId();
            tagIds[i] = tagId;
            CmsTag cmsTag = cmsTagMapper.selectCmsTagByTagId(tagId);
            tags.add(cmsTag);
        }
        blog.setTagIds(tagIds);
        blog.setTags(tags);
        //查询分类列表
        List<CmsBlogType> blogTypeList = cmsBlogTypeMapper.selectBlogTypeList(id);
        Long[] typeIds = new Long[blogTypeList.size()];
        for (int i = 0; i<blogTypeList.size();i++){
            CmsBlogType cmsBlogType = blogTypeList.get(i);
            Long typeId = cmsBlogType.getTypeId();
            typeIds[i] = typeId;
            CmsType cmsType = cmsTypeMapper.selectCmsTypeByTypeId(typeId);
            types.add(cmsType);
        }
        blog.setTypeIds(typeIds);
        blog.setTypes(types);
        return blog;
    }

    /**
     * 查询博客信息列表
     *
     * @param cmsBlog 博客信息
     * @return 博客信息
     */

    public List<CmsBlog> selectCmsBlogList(CmsBlog cmsBlog)
    {
        return cmsBlogMapper.selectCmsBlogList(cmsBlog);
    }

    /**
     * 新增博客信息
     *
     * @param cmsBlog 博客信息
     * @return 结果
     */

    public int insertCmsBlog(CmsBlog cmsBlog)
    {
        cmsBlog.setCreateTime(DateUtils.getNowDate());
        return cmsBlogMapper.insertCmsBlog(cmsBlog);
    }

    /**
     * 修改博客信息
     *
     * @param cmsBlog 博客信息
     * @return 结果
     */

    public int updateCmsBlog(CmsBlog cmsBlog)
    {
        cmsBlog.setUpdateTime(DateUtils.getNowDate());
        return cmsBlogMapper.updateCmsBlog(cmsBlog);
    }

    /**
     * 批量删除博客信息
     *
     * @param ids 需要删除的博客信息主键
     * @return 结果
     */

    public int deleteCmsBlogByIds(Long[] ids)
    {
        return cmsBlogMapper.deleteCmsBlogByIds(ids);
    }

    /**
     * 删除博客信息信息
     *
     * @param id 博客信息主键
     * @return 结果
     */

    public int deleteCmsBlogById(Long id)
    {
        return cmsBlogMapper.deleteCmsBlogById(id);
    }

    /*23-7-27*/

    /**
     * 按分类查询文章列表
     */
    public List<CmsBlog> selectCmsBlogListByTypeId(Long id) {
        List<CmsBlog> cmsBlogList = cmsBlogMapper.selectCmsBlogListByTypeId(id);
        //List<CmsBlog> blogList = BlogListAddTypeAndTag(cmsBlogList);
        return cmsBlogList;
    }

    /**
     * 按标签查询文章列表
     */

    public List<CmsBlog> selectCmsBlogListByTagId(Long id) {
        List<CmsBlog> cmsBlogList = cmsBlogMapper.selectCmsBlogListByTagId(id);
        //List<CmsBlog> blogList = BlogListAddTypeAndTag(cmsBlogList);
        return cmsBlogList;
    }

    /**
     * 查询推荐文章列表
     */

    public List<CmsBlog> selectCmsBlogListRecommend(CmsBlog cmsBlog) {
        List<CmsBlog> cmsBlogList = cmsBlogMapper.selectCmsBlogListRecommend(cmsBlog);
        //List<CmsBlog> blogList = BlogListAddTypeAndTag(cmsBlogList);
        return cmsBlogList;
    }

}
